package com.handc.springcloud.controller;


import com.handc.springcloud.pojo.Student;
import com.handc.springcloud.service.StudentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/stu/get/{id}")
    //一旦调用服务方法失败抛出 错误信息后 自动调用@HystrixCommand标注好的fallbackMethod指定的方法
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Student getById(@PathVariable("id") Long id) {
        Student student = studentService.queryById(id);
        //模拟查询异常
        if (student == null) {
            throw new RuntimeException("id: " + id + "不存在该用户,或者无法找到信息");

        }
        return student;

    }

    //备选方法
    public Student hystrixGet(Long id) {
        return new Student()
                .setId(id)
                .setName("id: " + id + "没有对应的信息, null---->@Hystrix")
                .setDb_name("no this databases in MySQL");
    }


}
